# python中的高阶函数 函数可以作为变量名
def add(x, y, f):
    return f(x) + f(y)


print(add(-5, 9, abs))  # 14


def f(x):
    return x * x


# python中map()函数
print(list(map(f, [1, 2, 3])))

import functools


# reduce()对list的每个元素反复调用函数f，并返回最终结果值。
def prod(x, y):
    return x * y


print(functools.reduce(prod, [2, 4, 5, 7, 12]))

import math


def is_sqr(x):
    return math.sqrt(x) % 1 == 0


print(list(filter(is_sqr, range(1, 101))))


# 装饰器函数
def new_fn(y):
    def fn(x):
        print('call ' + y.__name__ + '()')
        return y(x)

    return fn


@new_fn
def f1(x):
    return x * 2


print(f1(5))

import time


def performance(f):
    def fn(*args, **kwargs):
        print('call', f.__name__, '() in ', time.strftime('%Y-%m-%d', time.localtime(time.time())))
        return f(*args, **kwargs)

    return fn


@performance
def f2(x):
    return x * 2


print(f2(1))
